import store from "../store";
const base_url = "http://run.4hl.cn/addons/sd_phone/core/api.php/?s=",
	_uniacid = "15",
	_acid = "15";
// 请求
async function get(opt) {

}
async function post(opt) {
	// console.log("post")
	
	if(!store.state.loading){
		store.commit("switch_loading","true");
		console.log( opt.url+" --> 打开loading")

	}
	let token = uni.getStorageSync("token");
	if (opt.data) {
		opt.data._uniacid = _uniacid
		opt.data._acid = _acid
		opt.data.access_token = token?token:""
	}else{
		opt.data = {};
		opt.data._uniacid = _uniacid
		opt.data._acid = _acid
		opt.data.access_token =  token?token:""
	}
	return new Promise(function(resolve,reject){
		uni.request({  
			url: base_url + opt.url, //仅为示例，并非真实接口地址。
			data: opt.data,
			method:"POST",
			success: (res) => {
				if(res.statusCode==200){
					resolve(res.data)
				}
			 
			},
			complete() {
				console.log( opt.url+" --> 关闭loading")

				store.commit("switch_loading","false")
			}
		});
	})
	
}

// 获取验证码
async function get_msg_code(phone) {
	store.commit("switch_loading","true");
	let token = uni.getStorageSync("token");
	let data = {};
	data._uniacid = _uniacid
	data._acid = _acid
	data.access_token =  token?token:"";
	data.phone = phone
	return new Promise(function(resolve,reject){
		uni.request({  
			url: base_url + "/Sendmessage/send_sms", //仅为示例，并非真实接口地址。
			data,
			method:"POST",
			success: (res) => {
				if(res.statusCode==200){
					resolve(res.data)
				}
			 
			},
			complete() {
				store.commit("switch_loading","false")
			}
		});
	})
}
// 上传图片
async function upload_img() {

}
// 经纬度转地址
async function position_to_address(lng,lat){
	store.commit("switch_loading","true");
	// https://apis.map.qq.com/ws/geocoder/v1/?location=39.984154,116.307490&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77&get_poi=1
	return new Promise(function(resolve,reject){
		uni.request({
			url:"https://apis.map.qq.com/ws/geocoder/v1/",
			method:"GET",
			data:{
				location:lat+','+lng,
				key:"BONBZ-LYUWJ-KKEFR-KDWUP-PQHL5-QUFBI"
			},
			success(res) {
				
				if(res.data.status==0){
					resolve(res.data.result)
				}
			},
			complete() {
				store.commit("switch_loading","false")
			}
		})
	})
	
}
let obj = {

	get,
	post,
	get_msg_code,

	upload_img,
	position_to_address

}


export default obj
